package org.cangku.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

@Controller
@RequestMapping("/shangchuan")
public class Shangchuan {
    @RequestMapping(value = "tupian" ,method = RequestMethod.POST)
    public String addStock(@RequestParam(value = "multipartFile", required = false) MultipartFile[] multipartFile, HttpServletRequest request) throws IOException {
        /*System.out.println(multipartFile);*/
        for (MultipartFile files : multipartFile) {
            //1.获取文件名字
            String originalFilename = files.getOriginalFilename();
            System.out.println(originalFilename + "++++++++++++++++++++");
//获取不会重复的毫秒数
            //long l = System.currentTimeMillis();
//新名字
            //String newName = l + originalFilename;
            System.out.println(originalFilename + "----------++++++");
//2.图片的输入流名字
            try {
                InputStream inputStream = files.getInputStream();
//3.存在于项目中的路径
                String wlPath = "F:\\ideaxiti\\CangKu\\src\\main\\webapp\\image/" + originalFilename;
//4.逻辑路径，存到数据库中
                String ljPath = "../image/" + originalFilename;
//5.临时路径
                String realPath = request.getSession().getServletContext().getRealPath("/");
                String lsPath = realPath + "//image//" + originalFilename;

//6.如果没有那个文件夹就创建
                File wlFile = new File("F:\\ideaxiti\\CangKu\\src\\main\\webapp\\image");
                if (!wlFile.exists()) {
                    wlFile.mkdir();
                }
                File isFile = new File(realPath + "\\image\\");
                if (!isFile.exists()) {
                    isFile.mkdir();
                }
//7.不为空时
                if (!files.isEmpty()) {
                    //输出流 写物理路径
                    FileOutputStream wlStream = new FileOutputStream(wlPath);
                    //输出流 写临时路径
                    FileOutputStream lsStream = new FileOutputStream(lsPath);
                    int len = 0;
                    while ((len = inputStream.read()) != -1) {
                        wlStream.write(len);//写入
                        lsStream.write(len);

                    }
                    wlStream.flush();
                    lsStream.flush();

                    wlStream.close();
                    lsStream.close();

                    inputStream.close();

                }
                System.out.println("存到文件夹位置："+wlPath);
                System.out.println("数据库字段："+ljPath);
                System.out.println("临时路径："+lsPath);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
       /* System.out.println(multipartFile + "-------------------------------");
        System.out.println(file + "-------------------------------");*/


        /*stock.setStockPhoto(ljPath);
        stockService.addStock(stock);*/

        return  "xitong/imgShangchuan";

    }
}
